#!/bin/bash

# shell exec directory
SHELL_DIR=$(
  cd "$(dirname "$0")"
  pwd
)
# shell exec arguments
opt="${@:2}"

cd $SHELL_DIR

echo "shell dir: $SHELL_DIR, arguments:$opt"

setup() {
  echo "setup starting..."
  mvn clean verify -Dmaven.test.skip=true && ./setup-cli/setup-cli db create && ./setup-cli/setup-cli db migrate && ./setup-cli/setup-cli db seed
  echo "done!"
}

reset() {
  echo "reset starting..."
  mvn clean verify -Dmaven.test.skip=true && ./setup-cli/setup-cli db drop && ./setup-cli/setup-cli db create && ./setup-cli/setup-cli db migrate && ./setup-cli/setup-cli db seed
  echo "done!"
}

migrate() {
  echo "migrate starting..."
  mvn clean verify -Dmaven.test.skip=true && ./setup-cli/setup-cli db migrate
  echo "done!"
}

seed() {
  echo "migrate starting..."
  mvn clean verify -Dmaven.test.skip=true && ./setup-cli/setup-cli db seed
  echo "done!"
}

jooq() {
  echo "jooq starting..."
  mvn clean verify -Dmaven.test.skip=true && ./setup-cli/setup-cli gen jooq
  echo "done!"
}

migrateJooq() {
  echo "migrate and jooq starting..."
  mvn clean verify -Dmaven.test.skip=true && ./setup-cli/setup-cli db migrate && ./setup-cli/setup-cli gen jooq
  echo "done!"
}

build() {
  echo "build skip test starting..."
  mvn clean verify -Dmaven.test.skip=true
  echo "done!"
}

_test() {
  echo "test starting..."
  mvn clean test
  echo "done!"
}

echo "Running $1 command as $USER"

case "$1" in
'setup')
  setup
  ;;
'reset')
  reset
  ;;
'migrate')
  migrate
  ;;
'seed')
  seed
  ;;
'jooq')
  jooq
  ;;
'build')
  build
  ;;
'test')
  _test
  ;;
'migrateJooq')
  migrateJooq
  ;;
*)
  echo "Usage: $0 {setup|reset|migrate|seed|jooq|build|test|migrateJooq}"
  exit 1
  ;;
esac
exit 0
